package ac.manchester.cs.afzal.autoservd.parser_handler;

import ac.manchester.cs.afzal.autoservd.file_handler.File_Manager;
import ac.manchester.cs.afzal.autoservd.regular_expressions.String_Manipulator;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.LinkedList;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.Properties;

/**
 * Title: Term Classifier
 *
 * Description: A toolkit to find the relevance of candidate terms from a text corpus
 * to any particular topic/sub-domain using a term classification driven approach. The
 * system utilizes the lexical and contextual profiles of the candidate and domain-representing
 * "Resource Terms" (Seed and Ontological). The lexical and contextual profiles of candidate terms
 * are compared with those of Resource Terms to measure their relevance to the topic of interest.
 *
 * TreeConversion class converts the
 * <p>Company: The University of Manchester</p>
 *
 * @author Hammad Afzal
 * @version 1.0
 *
 */
public class TreeConversion
{

    private Properties props;
    private String MODE = "MODE";
    private String PARSED_SENTENCES_CORPUS = "PARSED_SENTENCES_CORPUS";
    private String XML_SENTENCES_CORPUS = "XML_SENTENCES_CORPUS";
    private String PARSED_SENTENCES_TRAINING = "PARSED_SENTENCES_TRAINING";
    private String XML_SENTENCES_TRAINING = "XML_SENTENCES_TRAINING";
    public final static String xmlDeclaration = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>";
    public final static String xmlSchema = "<corpus-info xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"corpus.xsd\">";
    File_Manager fileManager = new File_Manager();
    String_Manipulator strMan = new String_Manipulator();

    public TreeConversion()
    {
        try
        {
            props = new Properties();
            props.load(new FileInputStream("Term_Classification.properties"));
            XML_SENTENCES_CORPUS = props.getProperty(XML_SENTENCES_CORPUS);
            PARSED_SENTENCES_CORPUS = props.getProperty(PARSED_SENTENCES_CORPUS);
            XML_SENTENCES_TRAINING = props.getProperty(XML_SENTENCES_TRAINING);
            PARSED_SENTENCES_TRAINING = props.getProperty(PARSED_SENTENCES_TRAINING);
            MODE = props.getProperty(MODE);
        }
        catch (FileNotFoundException ex)
        {
            Logger.getLogger(TreeConversion.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (IOException ex)
        {
            Logger.getLogger(TreeConversion.class.getName()).log(Level.SEVERE, null, ex);
        }


    }

    /**
     * Converts brackets in PennTreeFormat to XML Tags
     */
    public void brackets_to_tags()
    {
        String[] list_dirs, list_doc;

        File input_file_dir = MODE.equals("Corpus") ? new File(PARSED_SENTENCES_CORPUS) : new File(PARSED_SENTENCES_TRAINING);
        list_dirs = input_file_dir.list();

        for(String dir_name : list_dirs)
        {
            File mkdir = MODE.equals("Corpus") ? new File(XML_SENTENCES_CORPUS + "//" + dir_name) : new File(XML_SENTENCES_TRAINING + "//" + dir_name);
            mkdir.mkdir();

            File file_doc = MODE.equals("Corpus") ? new File(PARSED_SENTENCES_CORPUS + "//" + dir_name) : new File(PARSED_SENTENCES_TRAINING + "//" + dir_name);
            list_doc = file_doc.list();

            for(String file_name : list_doc)
            {
                StringBuffer strBufWritor = new StringBuffer();
                StringBuffer file_contents_buffer = new StringBuffer();
                String contents;

                if(MODE.equals("Corpus"))
                {
                    file_contents_buffer = fileManager.fileReader(PARSED_SENTENCES_CORPUS +
                            "//" + dir_name + "//" + file_name);
                }
                else
                {
                    file_contents_buffer = fileManager.fileReader(PARSED_SENTENCES_TRAINING +
                            "//" + dir_name + "//" + file_name);
                }

                contents = file_contents_buffer.toString();

                int count = 0;
                LinkedList element = new LinkedList();


                strBufWritor.append(xmlDeclaration);
                strBufWritor.append(System.getProperty("line.separator"));
                strBufWritor.append(xmlSchema);
                strBufWritor.append(System.getProperty("line.separator"));

                StringTokenizer st1 = new StringTokenizer(contents.toString(),
                        " ");
                while(st1.hasMoreElements())
                {
                    String term = st1.nextToken();
                    if(term.startsWith("("))
                    {

                        strBufWritor.append("<st_" +
                                term.substring(1, term.length()) +
                                ">");
                        element.add(term.substring(1, term.length()));
                        count ++;
                    }
                    else if(term.contains(")"))
                    {
                        int index = term.indexOf(")");
                        int length = term.substring(index,
                                term.lastIndexOf(")")).length();
                        strBufWritor.append(term.substring(0, index));
                        int j = length + 1;
                        try
                        {
                            while(j > 0)
                            {
                                strBufWritor.append("</st_" +
                                        element.getLast().toString() + ">");
                                strBufWritor.append(System.getProperty(
                                        "line.separator"));
                                element.removeLast();
                                j --;
                            }
                        }
                        catch (Exception ex)
                        {
                        }
                    }
                }
                strBufWritor.append("</corpus-info>");
                strBufWritor.append(System.getProperty("line.separator"));
                String file_contents = strBufWritor.toString();

                file_contents = strMan.pattern_replacer(file_contents, "/[*.*/]", "");
                file_contents = strMan.pattern_replacer(file_contents, "\\r", "");
                file_contents = strMan.pattern_replacer(file_contents, "\\n", "");
                file_contents = strMan.pattern_replacer(file_contents, "<st_,>,</st_,>", "");
                file_contents = strMan.pattern_replacer(file_contents, "<st_.>.</st_.>", "");
                file_contents = strMan.pattern_replacer(file_contents, "<st_''>''</st_''>", "");
                file_contents = strMan.pattern_replacer(file_contents, "<st_CC>&</st_CC>", "");
                file_contents = strMan.pattern_replacer(file_contents, "<st_ROOT>", "");
                file_contents = strMan.pattern_replacer(file_contents, "</st_ROOT>", "");
//                    file_contents = strMan.pattern_replacer(file_contents, "<st_S>", "");
//                    file_contents = strMan.pattern_replacer(file_contents, "</st_S>", "");

                String output_path = "";

                String output_file_name = fileManager.strip_file_name_from_file_name_plus_extension(file_name, "txt");
                output_file_name = output_file_name + ".xml";

                if(MODE.equals("Corpus"))
                {
                    output_path = XML_SENTENCES_CORPUS + "//" + dir_name + "//" + output_file_name;

                }
                else
                {
                    output_path = XML_SENTENCES_TRAINING + "//" + dir_name + "//" + output_file_name;
                }

                fileManager.fileWriter(file_contents, output_path);
                file_contents = "";
            }
        }
    }
}
